package com.petShop.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.petShop.models.Cart;

public class MyTool {
	
	public static String changeHtml(String source){
		return source
				.replace("&", "&amp;")
				.replace("<", "&lt;")
				.replace(">", "&gt;")
				.replace(" ","&nbsp;")
				.replace("\r\n", "<br/>");
	}
	public static String changeTime(Date date){
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return f.format(date);
	}
	public static int getCartAllcount(List<Cart> carts){
		int allCount = 0;
		for(Cart cart:carts){
			allCount += cart.getCount();
		}
		return allCount;
	}
	public static String setFilename(String kinds){
		String filename = "";
	    if(kinds.equals("1")){
	    	filename = "taidi";
	    }else if(kinds.equals("2")){
	    	filename = "hashiqi";
	    }else if(kinds.equals("3")){
	    	filename = "bianmu";
	    }else if(kinds.equals("4")){
	    	filename = "labuladuo";
	    }else if(kinds.equals("5")){
	    	filename = "samoye";
	    }else if(kinds.equals("6")){
	    	filename = "kejiquan";
	    }else if(kinds.equals("7")){
	    	filename = "bixiong";
	    }else if(kinds.equals("8")){
	    	filename = "jinmao";
	    }else if(kinds.equals("9")){
	    	filename = "zangao";
	    }else if(kinds.equals("101")){
	    	filename = "lingshi";
	    }else if(kinds.equals("102")){
	    	filename = "wanjv";
	    }else if(kinds.equals("103")){
	    	filename = "xihu";
	    }else if(kinds.equals("104")){
	    	filename = "meishi";
	    }else if(kinds.equals("105")){
	    	filename = "meizhuang";
	    }else if(kinds.equals("106")){
	    	filename = "wodian";
	    }else if(kinds.equals("107")){
	    	filename = "shipen";
	    }
		return filename;
		
	}
	public static void moveFile(String realPath,String[] goodImgs,String newfileName){
		System.out.println(goodImgs[0]);
		System.out.println(goodImgs[1]);
		System.out.println(goodImgs[2]);
		System.out.println(newfileName);
		
		String path=realPath+"resources\\images\\"+goodImgs[0]+"\\"+goodImgs[1]+"\\"+goodImgs[2];	
		String newPath=realPath+"resources\\images\\"+goodImgs[0]+"\\"+newfileName+"\\"+goodImgs[2];
		try {  
            File afile = new File(path);  
            if (afile.renameTo(new File(newPath))) {  
                System.out.println("File is moved successful!");  
            } else {  
                System.out.println("File is failed to move!");  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }
	}
	
	public static void deleteFile(String path){
		File file = new File(path);  
	    // 路径为文件且不为空则进行删除  
	    if (file.isFile() && file.exists()) {  
	        file.delete();  
	        System.out.println("File is delete successful!");    
	    }else{
	    	System.out.println("File is failed to delete!");  
	    }
	}

}
